#include "mdichild.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QApplication>
#include <QFileInfo>
#include <QFileDialog>
#include <QCloseEvent>
#include <QPushButton>

mdiChild::mdiChild(QWidget *parent) :
QTextEdit(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
    isUntitled = true;
}

void mdiChild::newFile()
{
    static int sequenceNumber = 1;
    curFile = tr("Untitled%1").arg(sequenceNumber++);
    setWindowTitle(curFile + "[*]" + tr(" - 多文档编辑器"));
    connect(document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
}

bool mdiChild::loadFile(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning(this, tr("多文档编辑器"), tr("文件读取失败%1:\n%2").arg(fileName).arg(file.errorString()));
        return  false;
    }
    QTextStream in(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    setPlainText(in.readAll());
    QApplication::restoreOverrideCursor();
    setCurrentFile(fileName);
    connect(document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
    return true;
}

QString mdiChild::userFriendlyCurrentFile()
{
    return QFileInfo(curFile).fileName();
}

bool mdiChild::save()
{
    if(isUntitled)
        return saveAs();
    return saveFile(curFile);
}

bool mdiChild::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("save as"), curFile);
    if(fileName.isEmpty())
        return false;
    return saveFile(fileName);
}

bool mdiChild::saveFile(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly | QFile::Text))
    {
        QMessageBox::warning(this, tr("多文档编辑器"), tr("文件写入失败%1:\n%2").arg(fileName).arg(file.errorString()));
        return false;
    }
    QTextStream out(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    out << toPlainText();
    QApplication::restoreOverrideCursor();
    setCurrentFile(fileName);
    return true;
}

void mdiChild::documentWasModified()
{
    setWindowModified(document()->isModified());
}

bool mdiChild::mayBeSave()
{
    if(document()->isModified())
    {
        QMessageBox box;
        box.setWindowTitle(tr("多文档编辑器"));
        box.setText(tr("文件%1是否要保存？").arg(userFriendlyCurrentFile()));
        box.setIcon(QMessageBox::Warning);
        QPushButton* yesButton = box.addButton(tr("&Yes"), QMessageBox::YesRole);
        box.addButton(tr("&No"), QMessageBox::NoRole);
        QPushButton* cancelButton = box.addButton(tr("Cancel"), QMessageBox::RejectRole);
        box.exec();

        if(box.clickedButton() == yesButton)
        {
            return save();
        }
        if(box.clickedButton() == cancelButton)
            return false;
    }
    return true;
}

void mdiChild::setCurrentFile(const QString &fileName)
{
    curFile = QFileInfo(fileName).canonicalFilePath();
    isUntitled = false;
    document()->setModified(false);
    setWindowModified(false);
    setWindowTitle(userFriendlyCurrentFile() + "[*]");
}


void mdiChild::closeEvent(QCloseEvent *event)
{
    if(mayBeSave())
        event->accept();
    else {
        event->ignore();
    }
}
